/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plagiadetector.datatype;

import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Class for PAN 2011 Corpus objects.
 * @author Dell
 */
public class PANCorpus {
    public String address; //file address
    public String refaddress; // *.txt adress
    
    public String authors; //file original author
    public String language; 
    public String title;
    public String md5value; //md5 hash value
    
    public ArrayList<PANCase> plagCases; //list of real plagiarism cases
    
    public PANCorpus(){
        plagCases = new ArrayList<PANCase>();
    }
    
    public PANCorpus(String addr, String refaddr, String auth, String lang, String title, String md5val){
        address = addr;
        refaddress = refaddr;

        authors = auth;
        language = lang;
        this.title = title;
        md5value = md5val;
        
        plagCases = new ArrayList<PANCase>();
    }
    
    /**
     * Add a PAN Case to this object.
     * @param name name of the case.
     * @param type type of the case.
     * @param obfuscated whether the case is obfuscated or not.
     * @param lang language of the case.
     * @param offset initial character number where the case is started.
     * @param length  length of the case (in characters).
     */
    public void addPANCase(String name, String type, boolean obfuscated, String lang, int offset, int length){
        plagCases.add(new PANCase(name, type, obfuscated, lang, offset, length));
    }
    
    /**
     * get a PANCase respective with given index number.
     * @param i PANCase index number.
     * @return PANCase object.
     */
    public PANCase getPANCase(int i){
        return (PANCase) plagCases.get(i);
    }
    /**
     * get the number of PANCases stored in this object.
     * @return number of PANCases.
     */
    public int getNumPANCase(){
        return plagCases.size();
    }
    
    /**
     * Print this object info.
     */
    public void printCorpusInfo(){
        System.out.println("XML file address : "+address);
        System.out.println("Text file address : "+refaddress);
        System.out.println("Author : "+authors);
        System.out.println("Title : "+title);
        System.out.println("Language : "+language);
        System.out.println("MD5 Hash Value : "+md5value);
        System.out.println("Cases :");
        for(int i=0; i<plagCases.size(); i++){
            ((PANCase)plagCases.get(i)).printCase();
            System.out.println("+++++++++++++++++++");
        }
        System.out.println("----------------------------------------------------");
    }
    
    /**
     * Get the text of this corpus.
     * @return 
     */
    public ArrayList<String> getCorpusTextArrayList(){
        ArrayList<String> corpText = new ArrayList<String>();
        int linenum = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(refaddress));
            while(br.ready()){
                if(linenum == 0){
                    corpText.add(br.readLine().substring(1));
                }else{
                    corpText.add(br.readLine());
                }
                linenum++;
            }
            br.close();
            br = null;
        }  catch (IOException ex) {
                Logger.getLogger(PANCorpus.class.getName()).log(Level.SEVERE, null, ex);
        }
        return corpText;
    }
/*    
    public String getCorpusText(){
        StringBuilder buff = new StringBuilder("");
        ArrayList<String> x = getCorpusTextArrayList();
        for(String s : x){
            buff.append(" ").append(s);
        }
        return buff.toString();
    }
*/
    public String getCorpusText(){
        StringBuilder buff = new StringBuilder();
        int linenum = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(refaddress));
            while(br.ready()){  
//                if(linenum == 0){
//                    buff.append(br.readLine().substring(1)).append(" "); //to handle first strange character occurence (?)
//                }else{
                    buff.append(br.readLine()).append("\n");
//                }
                linenum++;
            }
            br.close();
            br = null;
        }  catch (IOException ex) {
                Logger.getLogger(PANCorpus.class.getName()).log(Level.SEVERE, null, ex);
        }
        return buff.toString();
    }
    
}
